package com.zj.typeFilter;

import com.zj.annotations.District;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.ClassMetadata;
import org.springframework.core.type.MethodMetadata;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.core.type.filter.TypeFilter;

import java.io.IOException;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class MyTypeFilter2 implements TypeFilter {

    private static String districtName;

    static {

        try {
            Properties properties = PropertiesLoaderUtils.loadAllProperties("district.properties");
            districtName = properties.getProperty("common.district");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @Override
    public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException {
        //获取当前类的注解信息
        AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata();
        //获取当前类的类信息
        ClassMetadata classMetadata = metadataReader.getClassMetadata();
        //获取当前类的资源、路径信息
        Resource resource = metadataReader.getResource();
//        Set<MethodMetadata> districts = annotationMetadata.getAnnotatedMethods("District");
//        for (MethodMetadata a:districts) {
//            System.out.println(a.getMethodName()+"==========");
//        }
        Map<String, Object> annotationMaps = annotationMetadata.getAnnotationAttributes(District.class.getName());
        System.out.println("annotationMaps:"+annotationMaps);
        String className = classMetadata.getClassName();
        try {
            Class<?> aClass = Class.forName(className);
            District annotation = aClass.getAnnotation(District.class);
            return annotation == null ? false : !districtName.equalsIgnoreCase(annotation.value());

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }


        return false;
    }
}
